package com.hmall.order.listener;

import com.hmall.clients.ItemClient;
import com.hmall.order.common.MQConstants;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.pojo.Item;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Mingxi Guo
 * @Create 2022-07-18 9:05
 * @Email guomingxi2018@163.cm
 */
@Component
public class MQListener {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;
     @Autowired
     private ItemClient itemClient;

    @RabbitListener(queues = MQConstants.DELAY_QUEUE)
    public void listenOrder(String orderDetailId){

        OrderDetail orderDetail = orderDetailMapper.selectById(orderDetailId);
        Long orderId = orderDetail.getOrderId();
        Order order = orderMapper.selectById(orderId);
        //订单已支付则丢弃，已经取消也不处理（幂等）
        if (order.getStatus() != 1 || order.getStatus() ==5){
            return;
        }
        Integer num = orderDetail.getNum();
        Long itemId = orderDetail.getItemId();
         //恢复库存
        Item itemDB = itemClient.getById(itemId);
        itemDB.setStock(itemDB.getStock()+num);
        itemClient.update(itemDB);


    }
}
